/* eslint-disable operator-linebreak */
import { defineStore } from 'pinia';
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus';
import { getUserPermission, getUserProject } from '@/api/user';
import { setAsyncRoutes, routerToMenu } from '@/utils/user';
import {
  getRouteToggle,
  setRouteToggle
} from '@/utils/auth';

interface Permissions {
  buttons: string; // 按钮权限
  datas: any[]; // 数据权限
}

const useUserStore = defineStore('user', {
  state: () => ({
    info: {} as any,
    menus: {},
    permissions: {} as Permissions,
    getMenus: [],
    firstMenu: [],
    projectOption: []
  }),
  actions: {
    getRoute() {
      const router = useRouter()
      // console.log('router', router)
      console.log('getRouteToggle()', getRouteToggle())
      console.log('menus', this.menus)
      this.menus.map((item:any) => {
        // debugger
        const routeIndex = getRouteToggle()
        if (item.path.split('/')[1] === routeIndex) {
          this.getMenus = item.children
        } 
        // if (routeIndex == 'undefined' && item.id === router.currentRoute.value.matched[0].name) {
        //   this.getMenus = item.children
        // }
        return this.getMenus
      })
      console.log('getMenus', this.getMenus)
      return this.getMenus
    },
    // router to menu
    setMenu(routers: []) {
      const menus: any[] = [];
      routerToMenu(menus, routers);
      // two
      this.menus = menus;
    },
    // 用户中心返回的权限处理
    getUserPermission() {
      return new Promise((resolve, reject) => {
        getUserPermission()
          .then((res: any) => {
            const {
              // buttonPermissions,
              // dataPermissions,
              menuPermissions,
            } = res.data?.userPermission;
            this.info.userName = res.data?.userName;
            this.info.orgName = res.data?.orgName;
            this.info.orgLogo = res.data?.orgLogo;
            // const buttons = buttonPermissions
            //   .map((item: any) => item.code)
            //   .join(',');
            // this.permissions = {
            //   buttons,
            //   datas: dataPermissions,
            // };
            // step one get async routes menuPermissions
            const { menus, routes } = setAsyncRoutes(menuPermissions);
            // TODO async routes set menus two times or more 
            this.menus = menus;
            resolve({ routes, ...res.data });
          })
          .catch((error) => {
            ElMessage.error(error.msg);
            reject(error);
          });
      });
    },
    getUserProject() {
      return new Promise((resolve, reject) => {
        getUserProject()
          .then((res: any) => {
            this.projectOption = res.data
          })
          .catch((error) => {
            ElMessage.error(error.msg);
            reject(error);
          });
      });
    },
    getTopMenuList() {
      this.firstMenu = [
        { code: '1', menuName: 'settle', id: 1, title: '抄表结算' },
        { code: '2', menuName: 'collect', id: 2, title: '采集管理' },
        { code: '3', menuName: 'system', id: 3, title: '系统管理' },
      ]
      // debugger
      // setRouteToggle((this.firstMenu && this.firstMenu.length > 0) ? this.firstMenu[0].menuName : 'form')
    },
  },
});

export default useUserStore;
